package cn.kgc.ssm.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.util.ObjectUtil;
import cn.kgc.ssm.dto.QueryStudent;
import cn.kgc.ssm.entity.Student;
import cn.kgc.ssm.mapper.StudentMapper;
import cn.kgc.ssm.service.StudentService;
import cn.kgc.ssm.util.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * @author 课工场
 * @date 2023/10/27
 * @description
 */
@Service
@Transactional
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;

    @Override
    public ResponseResult studentPage(QueryStudent queryStudent) {

        PageHelper.startPage(queryStudent.getPage(), queryStudent.getLimit());

        List<Student> students = studentMapper.getList(queryStudent.getStudentNo());

        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        long total = studentPageInfo.getTotal();

        return ResponseResult.success(students, total);
    }

    @Override
    public ResponseResult addStudent(Student student) {

        //  1. 判定学号是否已经存在
        Student stu = studentMapper.selectByStudentNo(student.getStudentNo());

        if (ObjectUtil.isNotEmpty(stu)) {
            return ResponseResult.success("学号已经存在", 115);
        }

        studentMapper.addStudent(student);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult getByStudentNo(String studentNo) {
        Student student = studentMapper.selectByStudentNo(studentNo);
        return ResponseResult.success(student, null);
    }

    @Override
    public ResponseResult editStudent(Student student) {


        studentMapper.editStudent(student);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult deleteStudent(Integer id) {

        studentMapper.deleteById(id);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult batchAdd(MultipartFile file) {

        try {
            InputStream inputStream = file.getInputStream();
            ImportParams importParams = new ImportParams();
            // 设置标题行数
            importParams.setTitleRows(1);
            // 设置表格头部行数  字段
            importParams.setHeadRows(1);

            /**
             * InputStream inputstream,
             * Class<?> pojoClass,       Student.class
             *  ImportParams params     导入参数
             */
            List<Student> students = ExcelImportUtil.importExcel(inputStream, Student.class, importParams);

            System.out.println("students = " + students);
            // 遍历集合 添加学生信息
            for (Student student : students) {

                Student stu= studentMapper.selectByStudentNo(student.getStudentNo());
                if (ObjectUtil.isNotEmpty(stu)){
                    throw  new  RuntimeException("学号已存在 学号:"+student.getStudentNo());
                }

                studentMapper.addStudent(student);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResponseResult.success();
    }

    @Override
    public List<Student> getStudentsByClazz(String clazz) {

        return studentMapper.getStudentByClazz(clazz);
    }
}
